#include "session.h"

namespace simple {
    Session::Session(int socketFd) : socket_fd_(socketFd), readerBuff_(socketFd), writerBuff_(socketFd) {}

    Session::Session(int socketFd, sockaddr_in *localAddr, sockaddr_in *remoteAddr) :
            socket_fd_(socketFd),
            readerBuff_(socketFd),
            writerBuff_(socketFd),
            local_addr_(localAddr),
            remote_addr_(remoteAddr) {}

    Session::~Session() {
        delete this->remote_addr_;
        delete this->local_addr_;
    }

    int Session::socketFd() const {
        return this->socket_fd_;
    }

    ReaderBuff *Session::readerBuff() {
        return &this->readerBuff_;
    }

    WriterBuf *Session::writerBuff() {
        return &this->writerBuff_;
    }

    const std::map<std::string, std::any> &Session::attribute() const {
        return this->attribute_;
    }

    bool Session::isAttributeExist(const std::string &key) noexcept {
        return this->attribute_.find(key) != this->attribute_.end();
    }

    bool Session::removeAttribute(const std::string &key) noexcept {
        if (this->isAttributeExist(key)) {
            this->attribute_.erase(key);
            return true;
        }
        return false;
    }

    void Session::resetAttribute() noexcept {
        this->attribute_.clear();
    }
}

